import {pass} from "@/utils/foo"
import store from "@/store/index"

export function processMoveAttribute(allowOut = false, vm, dom) {
  let startX, startY, offsetX, offsetY
  let moveSection = dom
  let move = moveSection.querySelector(".move-area")
  move.addEventListener("mousedown", function (e) {
    e.stopPropagation()
    store.commit("setWindowZIndex", vm.window.id)
    startX = e.clientX
    startY = e.clientY
    offsetX = moveSection.offsetLeft
    offsetY = moveSection.offsetTop
    this.isMoving = true
    window.addEventListener("mousemove", handleMoveMove)
    //eslint-disable-next-line no-unused-vars
    window.addEventListener("mouseup", e=>{
      window.removeEventListener("mousemove", handleMoveMove)
    })
  })
  function handleMoveMove(e) {
    if(vm.fullscreen){
      vm.fullscreen = false
      vm.$nextTick(() => {
        startX = e.clientX
        startY = e.clientY
        offsetX = e.clientX -  moveSection.clientWidth / 2
        moveSection.style.left = e.clientX - moveSection.clientWidth / 2  + "px"
      })

    }
    let deltaX = e.clientX - startX
    let deltaY = e.clientY - startY
    if(deltaX + offsetX <=0 && !allowOut){
      moveSection.style.left = "0"
      startX = e.clientX
      offsetX = 0
    }else if(deltaX + offsetX + moveSection.clientWidth > window.innerWidth && !allowOut){
      moveSection.style.left = window.innerWidth - moveSection.clientWidth + "px"
      startX = e.clientX
      offsetX = window.innerWidth - moveSection.clientWidth
    } else{
      moveSection.style.left = deltaX + offsetX + "px"
    }
    if(deltaY + offsetY <= 0 && !allowOut){
      moveSection.style.top = "0"
    }else if (deltaY + offsetY + moveSection.clientHeight > window.innerHeight && !allowOut) {
      pass()
    }else{
      moveSection.style.top = deltaY + offsetY + "px"
    }
  }
}
